// pages/order-confirm/order-confirm.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    failShow: false,
    successfulShow: false,
    detail: {},
    taocan_id: '',
    guige_id: '',
    couponList: [],
    shouhuo_area: '', //收货人地区
    shouhuo_detailarea: '', //收货人详细地址
    shouhuo_name: '', //收货人姓名
    shouhuo_tel: '', //收货人电话
    addr_id: '', //地址id
    coupon_name: '',
    money: '',
    coupon_id: '',
    order_price: '',
    mendian_arr: ['请选择时间', '周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    chose_time: 0, //选择时间
    order_id: '',
    guige_price: '',
    status: '', //优惠券状态
    coupon_type: '', //优惠券类型
    discount: '', //优惠券折扣
    manjian_money: '', //满多少可减
    currenttab: 0,
    shop_id: '',
    user_name: '',
    user_tel: '',
    shop_name: '',
    shop_addr: '',
    is_default: '',
    tjing:false,
    cutmoney:'',
  },


  //下单信息
  get_orderinfo() {
    let that = this;
    let data = {
      user_id: wx.getStorageSync('user_id'),
      taocan_id: that.data.taocan_id,
      guige_id: that.data.guige_id,
      addr_id: that.data.addr_id,
      order_id:that.data.order_id,
    }
    common.request(api.get_orderInfo, data, 'post').then((res) => {
      if (res.status == 1) {
        // var couponList = res.data.couponlist
        if (res.data.addrinfo) {
          var addr_id = res.data.addrinfo.addr_id;
          var shouhuo_area = res.data.addrinfo.shouhuo_area;
          var shouhuo_detailarea = res.data.addrinfo.shouhuo_detailarea;
          var shouhuo_name = res.data.addrinfo.shouhuo_name;
          var shouhuo_tel = res.data.addrinfo.shouhuo_tel;
          var is_default = res.data.addrinfo.is_default;
        } else {
          var addr_id = 0;
          var shouhuo_area = ''
          var shouhuo_detailarea = '';
          var shouhuo_name = '';
          var shouhuo_tel = '';
          var is_default = '';
        }

        if(res.data.taocaninfo.is_baoyou==1){
          var yunfei=parseFloat(res.data.taocaninfo.peisong_money)
        }else{
          var yunfei=0
        }
        var order_price=parseFloat(res.data.guigeinfo.guige_price)+yunfei;
        if(res.data.taocanorder){
          var ptime = res.data.taocanorder.ptime
          that.setData({
            taocan_id:res.data.taocanorder.taocan_id
          })
          
        }
        


        that.setData({
          detail: res.data,
          // currentTab: res.data.peisonginfo.peisong_type,
          addr_id: addr_id,
          // couponList: res.data.couponlist,
          shouhuo_area: shouhuo_area,
          shouhuo_detailarea: shouhuo_detailarea,
          shouhuo_name: shouhuo_name,
          shouhuo_tel: shouhuo_tel,
          couponList: res.data.couponlist,
          is_default: is_default,
          order_price: order_price,
          guige_price: res.data.guigeinfo.guige_price,
          yunfei:yunfei,
          chose_time:ptime||0,
          
        })
      }
    })
  },


  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e)
    this.setData({
      index: e.detail.value
    })
  },

  //支付成功 弹窗隐藏
  close() {
    this.setData({
      successfulShow: false
    })
  },
  bindMendianChange(e) {
    console.log(e.detail.value)
    this.setData({
      chose_time: e.detail.value
    })
  },

  //取消支付 弹窗隐藏
  canCel_fail() {
    this.setData({
      failShow: false
    })
  },

  //
  peisong_address() {
    wx.navigateTo({
      url: '/pages/address-info/address-info',
    })
  },
  peisong_address_ziti() {
    wx.navigateTo({
      url: '/pages/address-info/address-info?current=1',
    })
  },
  //选择优惠券
  choice_coupon() {
    // let detail = JSON.stringify(this.data.couponList)
    wx.navigateTo({
      url: '/pages/choice-coupon/choice-coupon?order_price=' + this.data.guige_price,
    })
  },

  // to_order_detail(){
  //   wx.navigateTo({
  //     url: '/pages/fahuo-detail/fahuo-detail',
  //   })
  // },
  //提交订单  并 z支付
  Add_order() {

    let that = this;
    let tjing=that.data.tjing
    // if (that.data.chose_time == 0) {
    //   wx.showToast({
    //     title: '请选择配送时间',
    //     icon: 'none'
    //   })
    //   return false
    // }
  if(!tjing){
    that.setData({
      tjing:true
    })

  
    
    let data = {
      order_id:that.data.order_id||0,
      taocan_id: that.data.taocan_id,
      guige_id: that.data.detail.guigeinfo.id,
      taocan_price: that.data.detail.guigeinfo.guige_price,
      order_price: that.data.order_price,
      coupon_id: that.data.coupon_id || 0,
      coupon_money: that.data.money || 0,
       ptime: 0,
      is_ziti: that.data.currenttab,
      addr_id: that.data.addr_id||'',
      shop_id: that.data.shop_id||'',
      peisong_money: that.data.yunfei,
      taocan_name: that.data.detail.taocaninfo.taocan_name,
      is_mianyou:that.data.currenttab,
    }
    console.log(data);


   
    common.request(api.AddOrder, data, 'post').then((res) => {
      if (res.status == 1) {
        that.setData({
          order_id: res.order_id
        })
        that.OrderBuy()
        that.setData({
          tjing:false
        })
      }else{
        common.showSuccessToast(res.msg)
        that.setData({
          tjing:false
        })
      }
    })
  }
  },

  //支付订单  
  OrderBuy() {
    let that = this;
    let data = {
      order_id: that.data.order_id
    }
    common.request(api.OrderBuy, data, 'post').then((res) => {
      if (res.status == 1) {
        console.log(res)
        let payInfo = res.jsapi;
        wx.requestPayment({
          timeStamp: payInfo.timeStamp,
          nonceStr: payInfo.nonceStr,
          package: payInfo.package,
          signType: payInfo.signType,
          paySign: payInfo.paySign,
          success(res) {
            that.setData({
              successfulShow: true
            })
          },
          fail(res) {}
        })
      }
    })
  },
  goindex(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  gotaocan(){
    wx.switchTab({
      url: '/pages/yuding/yuding',
    })
  },
  onLoad: function (options) {
    console.log(options.taocan_id, options.guige_id)
    this.setData({
      taocan_id: options.taocan_id,
      guige_id: options.guige_id,
      order_id: options.order_id||'',
    })
    this.get_orderinfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    const pages = getCurrentPages();
    const currPage = pages[pages.length - 1]; // 当前页
    // let newData = currPage.data.mydata;
    let couponData = currPage.data.mydata_coupon;
    // let zitiData = currPage.data.mydata_ziti;
    // console.log(zitiData)
    // console.log(newData)
    let yunfei=that.data.yunfei
    if (couponData)  {
      var coupon_id = couponData.coupon_id;
      var money = couponData.money;
      var discount = couponData.discount;
      var coupon_type = couponData.coupon_type;
      // var yunfei=(that.data.currenttab==0&&that.data.detail.taocaninfo.is_baoyou==1)?that.data.detail.taocaninfo.peisong_money:0;
      // var currenttab=that.data.currenttab
      // console.log(currenttab)
    }
    // if (zitiData)  {
    //   var shop_id = zitiData.shop_id;
    //   var user_name = zitiData.user_name;
    //   var user_tel = zitiData.user_tel;
    //   var shop_name = zitiData.shop_name;
    //   var shop_addr = zitiData.shop_addr;
    //   var currenttab = zitiData.currenttab;
    //   var yunfei=(currenttab==0&&that.data.detail.taocaninfo.is_baoyou==1)?that.data.detail.taocaninfo.peisong_money:0
    //   console.log(yunfei)
    // }
    // if (newData) {
    //   var shouhuo_area = newData.shouhuo_area;
    //   var shouhuo_detailarea = newData.shouhuo_detailarea;
    //   var shouhuo_name = newData.shouhuo_name;
    //   var shouhuo_tel = newData.shouhuo_tel;
    //   var id = newData.id;
    //   var is_default = newData.is_default;
    //   var currenttab = newData.currenttab;
    //   var yunfei=(currenttab==0&&that.data.detail.taocaninfo.is_baoyou==1)?that.data.detail.taocaninfo.peisong_money:0;
    //   console.log(currenttab==0&&that.data.detail.taocaninfo.is_baoyou==1)
    // }
    // console.log(newData);

    
    // console.log(shouhuo_area);

    // console.log(zitiData)
    // console.log(currenttab)
    // if(newData||zitiData||couponData){
      if(couponData){
      that.setData({
        // shouhuo_area: shouhuo_area,
        // shouhuo_detailarea: shouhuo_detailarea,
        // shouhuo_name: shouhuo_name,
        // shouhuo_tel: shouhuo_tel,
        // addr_id: id,
        // is_default: is_default,
        //优惠券信息
        coupon_id: coupon_id,
        money: money,
        discount: discount, // 优惠券折扣
        coupon_type: coupon_type, //优惠券类型
  
        //自提地址信息
        // shop_id: shop_id,
        // user_name: user_name,
        // user_tel: user_tel,
        // shop_name: shop_name,
        // shop_addr: shop_addr,
        // currenttab: currenttab||0,
        // yunfei:parseFloat(yunfei)
      })
      // console.log(that.data.currenttab)
  
      if (that.data.coupon_type == 0||that.data.coupon_type == 2) {
        var cutmoney =that.data.money    
      }else if (that.data.coupon_type == 1) {
        var cutmoney = that.data.guige_price * (1-that.data.discount / 10)
      }else{
        var cutmoney=0
      }
      var sumprice=(that.data.guige_price-cutmoney>0?that.data.guige_price-cutmoney:0)+parseFloat(yunfei)
      that.setData({
        order_price:sumprice,
        cutmoney:(cutmoney-that.data.guige_price>0?that.data.guige_price:cutmoney)
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})